﻿using MediatR;
using Microsoft.Extensions.Configuration;
using S.Common.helper;
using S.Initial.API.Application.Command.BreedingMothers;
using S.Initial.Domain;
using S.Initial.Infrastructure.Interface;
using static S.Initial.API.Application.Command.BreedingMothers.CreateBreedingMothersCommand;

namespace S.Initial.API.Application.CommandHandlers.BreedingMothers
{
    /// <summary>
    /// 批量添加
    /// </summary>
    public class CreateBreedingMothersCommandHandler : IRequestHandler<CreateBreedingMothersCommand, int>
    {
        private readonly IBreedingMothersRepository breeding;
        private readonly IConfiguration configuration;
        public CreateBreedingMothersCommandHandler(IBreedingMothersRepository breeding,IConfiguration configuration)
        {
            this.breeding = breeding;
            this.configuration = configuration;
        }
        public async Task<int> Handle(CreateBreedingMothersCommand request, CancellationToken cancellationToken)
        {
            UpdateEarTag updateEarTag = new UpdateEarTag();
            request.Breedmale.ForEach(a =>
            {
                updateEarTag = new UpdateEarTag()
                {
                    earTag = new List<string> { a.EarTag },
                    uflag = 2,
                    uState = 2,
                };
            });
            HttpClientHelper.PutDataResponse($"{configuration.GetValue<string>("Gateway")}/achs/api/SheepFileModel/SheepFileUpdateStatePL", updateEarTag);
            return await breeding.AddAllBreedingmothers(request.Breedmale);

        }
    }
}
